package com.dao.impl;

import com.bean.*;
import com.dao.*;
import com.util.*;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.*;
import org.junit.*;


import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TopicDaoImpl implements TopicDao {
    QueryRunner runner = new QueryRunner();
    Connection conn = null;
    String sql="";

    @Override
    public void insertTopic(String KuName, List<Topic> topics) throws Exception {
        conn= AliPool.getConn();

        //添加题目前的开始位置和添加后的总行数
        long begin=runner.query(conn, "select COUNT(topIcID) from bhtopic", new ScalarHandler<Long>());
        long end = (begin+topics.size());
        System.out.println("[Begin]"+begin);
        System.out.println("[E n d]"+end);
        //当前题库id
        int KuId = runner.query(conn, "select utID from usertopicku where tName=?", new ScalarHandler<Integer>(), KuName);
        System.out.println("[题库id]"+KuId);
        //添加题目
        sql="insert into bhtopic values(default, ?, ?, ?, ?, ?, ? , ?, ?, ?)";

        System.out.println("=====题库名字======="+KuName);
        System.out.println("=====题库ID======="+KuId);

        for (int i=0, j=(int)begin+1; i<topics.size(); i++, j++) {


            Topic topic = (Topic) topics.get(i);
            //如果找到题目则输出题目id
            if (runner.query(conn, "select topIcID from bhtopic where cPor=? && uid=?", new ScalarHandler<String>(), topic.getcPor(), topic.getuID()) != null) {
                String topicID = runner.query(conn, "select topIcID from bhtopic where cPor=? && uid=?", new ScalarHandler<>(), topic.getcPor(), topic.getuID()).toString();
                System.out.println("[========topicID=========]"+topicID);
                if(runner.query(conn, "select utID from userkutopic where TopIcID=?", new ScalarHandler<String>(), topicID) == topicID) {
                    System.out.println("==========continue===========");
                    continue;
                }
            }
            //添加题目到全部题目
            runner.update(conn, sql,
                topic.getuID(),
                topic.getcType(),
                topic.getcPor(),
                topic.getcTrue(),
                topic.getcA(),
                topic.getcB(),
                topic.getcC(),
                topic.getcD(),
                topic.getcNum());
            //吧题目与题目库表绑定
            runner.update(conn, "insert into userkutopic values(?, ?)", KuId, j);
//            Topic topic = (Topic)topics.get(j);
            System.out.println("["+j+"]"+topic.toString());

        }




    }

//    @Override
//    public void insertTopicKu(String uid, Topic topic) throws Exception {
//
//    }


    @Test
    public void t1() throws Exception {
        List<Topic> list = new ArrayList<Topic>();
        list.add(new Topic(2,"单选","4+2","D","4","11","54","6", 12));
        list.add(new Topic(2,"多选","14+2","A","16","11","5","64", 20));
        list.add(new Topic(2,"单选","2+2","B","14","4","4","5", 12));

//        list.add(new Topic(1,"单选","111114+2","A","谨慎思考","16","1","5","64"));
//        list.add(new Topic(2,"单选","111112+2","B","谨慎思考","14","543","4","5"));


        insertTopic("Mysql", list);


    }


}
